package com.example.tanloadpic.handle.impl;

import android.graphics.Bitmap;
import android.os.ParcelFileDescriptor;
import android.provider.MediaStore;
import android.widget.ImageView;

import com.example.tanloadpic.BitmapHunter;
import com.example.tanloadpic.RequestData;
import com.example.tanloadpic.TanLoadPic;
import com.example.tanloadpic.handle.RequestHandler;
import com.example.tanloadpic.utils.ImageResizer;

import java.io.File;
import java.io.FileDescriptor;
import java.io.IOException;
import java.io.InputStream;

import static com.example.tanloadpic.utils.constant.MEMORY_SOURCE;

public class MemoryCacheHandler implements RequestHandler {


    @Override
    public Bitmap load(TanLoadPic tanLoadPic, RequestData data) throws IOException {
        Bitmap bitmap = tanLoadPic.mMemoryCache.getBitmap(data.key);
        if (bitmap == null){/*
            ParcelFileDescriptor pf = TanLoadPic.instance.mContext.getContentResolver().openFile(data.uri,null,null);
            FileDescriptor fileDescriptor = pf.getFileDescriptor();
            bitmap = ImageResizer.decodeSampledBitmapFromFileDescriptor(fileDescriptor,50,50);*/

            bitmap =  MediaStore.Images.Media.getBitmap(TanLoadPic.instance.mContext. getContentResolver(), data.uri);
        }
        return bitmap;
    }

    @Override
    public String loadSource() {
        return MEMORY_SOURCE;
    }

    @Override
    public String toString() {
        return "MemoryCacheHandler{}";
    }
}
